<template>
  <div>
    <h1>餐厅列表</h1>
    <el-button type="primary" @click="openAddRestaurantDialog">添加餐厅</el-button>
    <el-dialog title="添加餐厅" v-model="dialogVisible" width="50%">
      <el-form :model="restaurant" label-width="120px">
        <el-form-item label="名称">
          <el-input v-model="restaurant.name"></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="restaurant.address"></el-input>
        </el-form-item>
        <el-form-item label="评分">
          <el-rate v-model="restaurant.rating" show-score allow-half></el-rate>
        </el-form-item>
        <el-form-item label="评论">
          <el-input type="textarea" v-model="restaurant.comments"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">提交</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-table :data="restaurants" style="width: 100%; margin-top: 20px;">
      <el-table-column prop="name" label="名称" width="180"></el-table-column>
      <el-table-column prop="address" label="地址" width="180"></el-table-column>
      <el-table-column prop="rating" label="评分" width="180"></el-table-column>
      <el-table-column prop="comments" label="评论"></el-table-column>
      <el-table-column label="操作" width="300">
        <template #default="scope">
          <div class="action-buttons">
            <el-button size="small" type="info" @click="viewDetails(scope.row.id)">详情</el-button>
            <el-button size="small" type="warning" @click="editRestaurant(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="deleteRestaurant(scope.row.id)">删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  name: 'RestaurantList',
  data() {
    return {
      dialogVisible: false,
      restaurant: {
        id: null,
        name: '',
        address: '',
        rating: null,
        comments: ''
      },
      restaurants: []
    };
  },
  created() {
    this.fetchRestaurants();
  },
  methods: {
    fetchRestaurants() {
      // 假设从后端获取数据
      this.restaurants = [
        {
          id: 1,
          name: '高档酒店餐厅',
          address: '芜湖市弋江区',
          rating: 4.5,
          comments: '很好吃',
          image: 'restaurant1.png'
        },
        {
          id: 2,
          name: '中档酒店餐厅',
          address: '芜湖市鸠江区',
          rating: 4.0,
          comments: '还可以',
          image: 'restaurant2.png'
        },
        {id: 3, name: '廊棚餐厅', address: '芜湖古镇', rating: 3.5, comments: '一般般', image: 'restaurant3.png'},
        {id: 4, name: '食堂', address: '安徽师范大学', rating: 4.8, comments: '太棒了', image: 'restaurant4.png'}
      ];
    },
    openAddRestaurantDialog() {
      this.resetForm();
      this.dialogVisible = true;
    },
    submitForm() {
      if (this.restaurant.id) {
        // 编辑现有餐厅
        const index = this.restaurants.findIndex(r => r.id === this.restaurant.id);
        if (index !== -1) {
          this.restaurants.splice(index, 1, {...this.restaurant});
        }
      } else {
        // 添加新餐厅
        const newId = this.restaurants.length ? Math.max(...this.restaurants.map(r => r.id)) + 1 : 1;
        this.restaurants.push({...this.restaurant, id: newId});
      }
      this.dialogVisible = false;
      this.resetForm();
    },
    resetForm() {
      this.restaurant = {
        id: null,
        name: '',
        address: '',
        rating: null,
        comments: ''
      };
    },
    viewDetails(id) {
      this.$router.push({name: 'RestaurantDetail', params: {id}});
    },
    editRestaurant(restaurant) {
      this.restaurant = {...restaurant};
      this.dialogVisible = true;
    },
    deleteRestaurant(id) {
      this.restaurants = this.restaurants.filter(r => r.id !== id);
    }
  }
};
</script>

<style scoped>
h1 {
  font-size: 24px;
  color: #333;
}

.el-button {
  margin-right: 10px;
}

.action-buttons {
  display: flex;
  justify-content: space-between;
}
</style>